/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.trainee.aplikasipenjualan.ui.servlet;

import com.trainee.aplikasipenjualan.entity.Barang;
import com.trainee.aplikasipenjualan.service.AplikasipenjualanService;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author BangsJack
 */

@SuppressWarnings("serial")
public class DaftarBarang extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext appCtx = WebApplicationContextUtils
        .getWebApplicationContext(req.getSession().getServletContext());

        AplikasipenjualanService service =
        (AplikasipenjualanService) appCtx.getBean("aplikasipenjualanService");

        List<Barang> semuaBarang = service.findAllBarang();
        
        resp.setContentType("text/html");
        
        String output = "<html>";
        output += "<head>";
        output += "<title>Daftar Barang</title>";
        output += "</head>";
        output += "<body>";
        output += "<h1>Daftar Barang</h1>";
        output += "<table>";
        output += "<tr>";
        output += "<td>Kode Barang</td>";
        output += "<td>Nama Barang</td>";
        output += "</tr>";

        for (Barang barang : semuaBarang) {
        output += "<tr>";
        output += "<td>"+barang.getKodeBrg()+"</td>";
        output += "<td>"+barang.getNamaBrg()+"</td>";

        output += "<td><a href='edit.php?id="+barang.getKodeBrg()+"'>edit</a>";
        output += " | <a href='delete.php?id="+barang.getKodeBrg()+"'>delete</a></td>";


        output += "</tr>";
        }


        output += "</table>";
        output += "</body>";
        output += "</html>";       
        
        resp.getWriter().print(output);
        resp.getWriter().close();
    }
    
}
